<?php
/**
 * @author HungPV <hungpv@tasvis.com.vn>
 * @deprecated 24/11/2012 version 1.0.0
 */
class users extends MX_Controller
{    
    function __construct() {
        parent::__construct();
    }
    
    /**
     * login()
     * @name Login function
     * @author HungPV <hungpv@tasvis.com.vn>
     * @deprecated Login action for user
     * 
     */
    function login()
    {
        $message = '';
        if($this->input->post('txtUsername'))
        {
            $user_login = $this->input->post('txtUsername');
            $user_pass = $this->input->post('txtPass');            
            if($this->checklogin($user_login,md5($user_pass)))
            {
                $user = new User();
                $user->where('user_login',$user_login)->get();
                if($user->user_status == 1)
                { 
                    $userdata = array(
                        'username'  => $user_login,
                        'login' => TRUE,
                        'user_id' => $user->id,
                        'user_activation_key'=>$user->user_activation_key                            
                    );
                    $this->session->set_userdata($userdata);  
                    redirect("home/index");

                }else{
                    $message = "Tài khoản của bạn chưa được kích hoạt!";
                }
            }else{
                $message = "Thông tin đăng nhập không đúng!";
            }
        }
        $data['message'] = $message;
        $this->common->load_template('users_login',$data);
    }
    
    function register()
    {
        if($this->input->post('txtUsername'))
        {
            $user_login= trim($this->input->post('txtUsername'));
            $user_pass= trim($this->input->post('txtPass'));            
            $user_nicename= trim($this->input->post('txtDisplayname'));
            $user_email= trim($this->input->post('txtEmail'));
            $user_address = $this->input->post('txtAddress');
            $user_phone = $this->input->post('txtPhone');
            $user_status= 0;

            if(!$this->checkExistUser($user_login,$user_email))
            {
                $user = new User();
                $user->user_login = $user_login;                

                $user->user_pass = md5($user_pass);                
                $user->user_nicename = $user_nicename;
                $user->user_email = $user_email;
                $user->user_registered = date('Y-m-d H:i:s');
                $user->display_name = $user_nicename;
                //$user2->authitem_id = 0;
                $user->user_status = $user_status;
                $user->address = $user_address;
                $user->phone = $user_phone;
                $user->save();

                $random_code = md5(uniqid(rand()));
                $valid = new Validate();
                $valid->confim_code=$random_code;
                $valid->login=$user_login;
                $valid->email=$user_email;
                $valid->save();
                
                //sendmail
                $option = new Option();
                $option->where('option_name','mail_server_host')->get();
                $host = $option->option_value; 
                
                $option = new Option();
                $option->where('option_name','mail_server_port')->get();
                $port = $option->option_value;  
                
                $option = new Option();
                $option->where('option_name','mail_server_username')->get();
                $usermail = $option->option_value;  
                
                $option = new Option();
                $option->where('option_name','mail_server_password')->get();
                $passmail = $option->option_value; 
               
               
               // cấu hình gửi mail 
                $config['protocol']='smtp';
                $config['smtp_host']=$host;
                $config['smtp_port']=$port;
                $config['smtp_timeout']='30';
                $config['smtp_user']=$usermail;
                $config['smtp_pass']=$passmail;
                $config['charset']='utf-8';
                $config['newline']="\r\n";
                $config['mailtype'] = 'html';
                $this->email->initialize($config);
                
                $this->email->from('dangky@tasvis.com','Đăng ký thành viên'); 
                $this->email->to($user_email);                                             
                $this->email->subject('Đăng ký thành viên');
                $email_msg='Chào mừng bạn đến với '.base_url().' <br/><br/>';
                $email_msg.='Tài khoản của bạn đã được đăng ký. <br/>Vui lòng click vào liên kết bên dưới để có thể đăng nhập vào hệ thống:<br>';
                $email_msg.=base_url().'users/validation/'.$user_login.'/'.$random_code;
                $this->email->message($email_msg);  
                $this->email->send();
                // Create wishlist restaurent
                // Tạo một danh mục nhà hàng yêu thích cho ng dùng này
                
                                
                redirect("users/registersuccess");
            }
            else {    
                if($this->checkExistUsername($user_login))
                {
                    $data['message1'] ='Người dùng đã tồn tại!';
                }
                else
                {
                    $data['message1'] = '';
                }
                if($this->checkExistUserEmail($user_email))
                {
                    $data['message2'] ='Email đã được sử dụng!';
                }
                else
                {
                    $data['message2'] = '';
                }
                 
                $this->common->load_template('users_register',$data);                
            }
            
        }else
        {
             
            $data['message1'] ='';
            $data['message2'] ='';
            $this->common->load_template('users_register',$data);            
        }       
    }
    function wishlist_restaurent()
    {
        $term = new Term();
        $term->name = 'admin';
        $term->slug = 'admin11';

        if($term->save())
        {    
            $last_term = new Term();
            $last_term->where('name','admin')->get();                    
            //add Term_taxonomy
            $term_taxonomy = new Term_taxonomy();                   
            //$term_taxonomy->term_id=$last_term->term_id;
            $term_taxonomy->taxonomy = 'wishlistrestaurent';
            $term_taxonomy->description = 'Nhà hàng yêu thích của admin';
            $term_taxonomy->user_id = 1;
            //$term_taxonomy->parent_term = $this->input->post('ddlTerm');
            $term_taxonomy->save($last_term);
        }      
    }
    function wishlist_product()
    {
        $term = new Term();
        $term->name = 'admin_product';
        $term->slug = 'admin11_product';

        if($term->save())
        {    
            $last_term = new Term();
            $last_term->where('name','admin_product')->get();                    
            //add Term_taxonomy
            $term_taxonomy = new Term_taxonomy();                   
            //$term_taxonomy->term_id=$last_term->term_id;
            $term_taxonomy->taxonomy = 'wishlistproduct';
            $term_taxonomy->description = 'Món ăn yêu thích của admin';
            $term_taxonomy->user_id = 1;
            //$term_taxonomy->parent_term = $this->input->post('ddlTerm');
            $term_taxonomy->save($last_term);
        }      
    }
    /**
     * validation
     * 
     * @author HungPV
     * @deprecated validate for user has registered.
     */
    function validation($username,$code)
    {
        $validate = new Validate();
        $count = $validate->where('confim_code',$code)
                         ->where('login',$username)
                        ->count();
        if($count>0)
        {
            //Update Status for User
            $user = new User();
            $user->where('user_login',$username)->get();
            $user->user_status = 1;
            $user->save();            
            
            $data['message']='';
            $this->common->load_template('users_validation',$data);           
        }
        else {
            //Update Status for User
            $user = new User();
            $user->where('user_login',$username)->get();
            $user->user_status = 1;            
             
            $data['message']='';
            $this->common->load_template('users_registerfalse',$data);          
           
        }        
    }    
    
    function registersuccess()
    {
        $data['message'] ='';
        $this->common->load_template('users_registersuccess',$data);            
    }
    
    /**
     * logout
     * @author HungPV <hungpv@tasvis.com.vn>
     * @deprecated Logout for user.
     */
    public function logout()
    {
        $this->session->sess_destroy();
        redirect("home/index");
    }
    
    /**
     * checklogin
     * @author HungPV <hungpv@tasvis.com.vn>
     * @param type $username
     * @param type $password
     * @return boolean
     * @deprecated Check User if exist, return true if exist, or false if not.
     */
    function checklogin($username,$password)
    {
        $user = new User();            
        $array = array('user_login'=>$username, 'user_pass'=>$password);
        $count = $user->where($array)->count();
        if($count == 0)
        {
            return false;
        }else{
            return true;

        }
    }
    
    /**
     * checkExistUser
     * 
     * @author HungPV
     * @deprecated Check if exist user in system 
     * 
     * @param String $username for Username
     * @param String $email for Email
     * @return boolean return true if exist, or return false if not.
     */
    function checkExistUser($username,$email)
    {
        $user = new User();
        return $user->checkExistUser($username, $email);
    }
    
    /**
     * 
     * @param type $username
     * @return type
     */
    function checkExistUsername($username)
    {
        $user = new User();
        return $user->checkExistUsername($username);
    }
    
    /**
     * 
     * @param type $email
     * @return type
     */
    function checkExistUserEmail($email)
    {
        $user = new User();
        return $user->checkExistUserEmail($email);
    }
    
    function account()
    {
        if($this->session->userdata('login')!=1)
        {
            redirect("users/login");
        }
        if($this->input->post('txtUsername'))
        {
            $user_id = $this->session->userdata('user_id');                       
            $user_nicename= trim($this->input->post('txtDisplayname'));
            $user_email= trim($this->input->post('txtEmail'));
            $user_address = $this->input->post('txtAddress');
            $user_phone = $this->input->post('txtPhone');
            
            $user = new User();
            $user->where('id',$user_id)->get();
            $user->user_nicename = $user_nicename;
            $user->user_email = $user_email;            
            $user->display_name = $user_nicename;           
            $user->address = $user_address;
            $user->phone = $user_phone;
            $user->save();           
        }
        
        $user_id = $this->session->userdata('user_id');
        $user = new User();
        $data['user'] = $user->where('id',$user_id)->get();
        $data['message']='';
        $this->common->load_template('users_account',$data);
    }
    
    function changepass()
    {
        if($this->session->userdata('login')!=1)
        {
            redirect("users/login");
        }
        $message = '';
        if($this->input->post('txtPass'))
        {
            $user_id = $this->session->userdata('user_id');                       
            $user_pass = $this->input->post('txtPass');
            $user_passnew = $this->input->post('txtPassNew');            
            
            $user = new User();
            $user->where('id',$user_id)->get();
            if($user->user_pass!=  md5($user_pass))
            {
                $message = 'Mật khẩu cũ không đúng!';
            }
            else
            {
                $user->user_pass = md5($user_passnew);
                $user->save();  
                $message = 'Đổi mật khẩu thành công!';
            }            
        }
        $data['message'] = $message;
        $this->common->load_template('users_changepass',$data);
    }
}
?>
